package trab4.transactions.two_phase_commit;

import sim.core.*;
import trab4.grid.GridOperations;
import trab4.rpc.*;
import trab4.transactions.AbstractTransactionManager;
import trab4.transactions.two_phase_commit.rpc_msgs.*;


public class Server_2PC_Stub extends ServerTransactionStub implements TwoPhaseCommitMessageHandler {
	
	TwoPhaseCommit impl ;
	
	public Server_2PC_Stub( EndPoint endpoint, AbstractTransactionManager<GridOperations> tm, TwoPhaseCommit impl ) {
		super( endpoint, tm ) ;
		this.impl = impl ;		
	}

	@Override
	@SuppressWarnings("unchecked")
	protected <T> T clientStub( EndPoint client) {
		return (T) new Client_2PC_Stub( client, server ) ;
	}
	
	public void onReceive( TcpChannel ch, Msg_2PC_OP1 m) {
		impl.tpc_op1() ;
		ch.tcpReply( new Msg_2PC_Return( 0 ) ) ;		
	}


	public void onReceive( TcpChannel ch, Msg_2PC_OP2 m) {
		Object res = impl.tpc_op2() ; ;
		ch.tcpReply( new Msg_2PC_Return( res ) ) ;		
	}

	
	public void onReceive( EndPoint src, Message m) {
		Thread.dumpStack() ;
	}
	
	public void onReceive( TcpChannel chn, Message m) {
		Thread.dumpStack() ;
	}
	
	public void onSendFailure( EndPoint dst, Message m) {
	}

	
}
